<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="searchForm.sid" class="filter-item" style="width: 110px;" clearable filterable :loading="loading" placeholder="选择区服">
        <el-option
          v-for="item in serverList"
          :key="item.sid"
          :label="item.name+'('+item.sid+')'"
          :value="item.sid"
        >
        </el-option>
      </el-select>
      <el-date-picker v-model="searchForm.startDate" class="filter-item" type="date" style="width: 160px;" placeholder="开始时间"></el-date-picker>-
      <el-date-picker v-model="searchForm.endDate" class="filter-item" type="date" style="width: 160px;" placeholder="结束时间"></el-date-picker>
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" :span-method="objectSpanMethod" border>
      <el-table-column label="日期" prop="date" width="100px" align="center"></el-table-column>
      <el-table-column label="充值区间" width="100px" align="center">
        <template slot-scope="{row}">
          {{ row.type | typeFilter }}
        </template>
      </el-table-column>
      <el-table-column label="1~6" prop="1" width="100px" align="center"></el-table-column>
      <el-table-column label="7~30" prop="7" width="100px" align="center"></el-table-column>
      <el-table-column label="31~100" prop="31" width="100px" align="center"></el-table-column>
      <el-table-column label="101~200" prop="101" width="100px" align="center"></el-table-column>
      <el-table-column label="201~500" prop="201" width="100px" align="center"></el-table-column>
      <el-table-column label="501~1000" prop="501" width="100px" align="center"></el-table-column>
      <el-table-column label="1001~2000" prop="1001" width="100px" align="center"></el-table-column>
      <el-table-column label="2001~3000" prop="2001" width="100px" align="center"></el-table-column>
      <el-table-column label="3001~5000" prop="3001" width="100px" align="center"></el-table-column>
      <el-table-column label="5001以上" prop="5001" width="100px" align="center"></el-table-column>
      <el-table-column label="总计" prop="total" width="100px" align="center"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getServerList } from '@/api/ops'
import { getOnlineDurationAvgList } from '@/api/statis'

const typeMap = {
  '1': '充值人数',
  '2': '平均时长'
}
export default {
  name: 'OnlineDurationAvg',
  filters: {
    typeFilter(type) {
      return typeMap[type]
    }
  },
  data() {
    return {
      list: [],
      hourList: [],
      serverList: [],
      loading: false,
      listLoading: false,
      searchForm: {
        sid: '',
        startDate: new Date(Date.now() - 86400000 * 10),
        endDate: new Date()
      }
    }
  },
  mounted() {

  },
  created() {
    this.getList()
    this.getServerList()
  },
  methods: {
    handleFilter() {
      this.getList()
    },
    async getList() {
      this.listLoading = true
      const res = await getOnlineDurationAvgList(this.searchForm)
      this.listLoading = false
      this.list = res.data
    },
    async getServerList() {
      const res = await getServerList()
      this.serverList = res.data.items
      this.loading = false
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (rowIndex % 2 === 0) {
          return {
            rowspan: 2,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    }
  }
}
</script>
